package teach.aistar.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import teach.aistar.model.vo.Result;
import teach.aistar.util.FileUtil;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;


/**
 * 本类用来演示:图片上传
 *
 * @author: success
 * @date: 2020/10/9 1:43 下午
 */
@Controller
@RequestMapping("/upload1")
public class UploadController {



//    @Controller
//    @RequestMapping("/upload")
//    public class UploadController {
@PostMapping("/uploads1")
@ResponseBody
public Result upload1(HttpServletRequest request,@RequestParam(value = "file") MultipartFile file){

    System.out.println(file.getOriginalFilename());
//    //获取imgs的路径
//    //Users/admin/Documents/success/teaching/wksp/springboot-j13/src/main/webapp/imgs/
//    String path = request.getServletContext().getRealPath("/imgs/");
//
//    //处理一下文件的名称
//    String filename = file.getOriginalFilename();
//    filename = UUID.randomUUID().toString()+"_"+filename;
//
//    System.out.println(filename+"====");
//
//    //System.out.println(filename);//8aa9b07e-abf7-4288-8d81-b0d975a9e57c_xx.png
//
//    //构建一个file实例
//    File f = new File(path+filename);
//    try {
//        //上传到本项目中的imgs目录
//        //file.transferTo(f);
//
//        //图片上传到nginx服务器
////        File f = new File("/usr/local/nginx/html/boot/imgs/"+filename);
//
    String path = request.getServletContext().getRealPath("/imgs/");
    System.out.println(FileUtil.FileUpload(path,file).getImage());



    return null;
}

    }